/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.deepsplink.interceptor {
	import org.splink.deepsplink.navigation.command.INavigationCommand;
	import org.splink.library.tree.Tree;

	/**
	 * Redirects requests for non existent pages to a page which actually exists
	 * 
	 * @author Max Kugland
	 */
	final public class InvalidPageInterceptor implements IInterceptor {
		private var _tree : Tree;
		private var _redirectId : String;
		private var _lastInterceptedId : String;
		private var _navigationCmd : INavigationCommand;

		public function InvalidPageInterceptor(navigationCmd : INavigationCommand, tree : Tree, redirectId : String) {
			_navigationCmd = navigationCmd;
			_tree = tree;
			_redirectId = redirectId;
		}
		
		/**
		 * @inheritDoc
		 */
		public function intercept(id : String, pageParams : Array) : Boolean {
			if(Tree.getNodeById(id, _tree.root) == null) {
				_lastInterceptedId = id;
				_navigationCmd.gotoPage(_redirectId);
				return true;
			} else {
				return false;
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function get lastInterceptedId() : String {
			return _lastInterceptedId;
		}
	}
}
